﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using ETWJ.Service;
using Microsoft.AspNetCore.Mvc;

namespace ETWJ.ERP.Controllers
{
    public class EmployeeController : Controller
    {
        int i = 4;
        public IEmployeeService<Employee, EmployeeDTO> employeeService { get; set; }
        //public EmployeeService employeeSer { get; set; }
        //public ICommissionService commonService { get; set; }

        public EmployeeController(IEmployeeService<Employee, EmployeeDTO> employeeService)
        {
            this.employeeService = employeeService;
        }

        public IActionResult Index()
        {
            return View();
        }


        public async Task<IActionResult> List(int pageIndex = 1)
        {
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }



            //var model = await employeeService.GetAll();// .GetByPage(pageIndex, 10);
            var model = await employeeService.GetByPage(pageIndex, 5);

            var totalCount = await employeeService.GetTotalCountAsync();
            var currentPageIndex = pageIndex;

            var pageCount = Math.Ceiling(totalCount / 5.0);

            ViewBag.PageCount = pageCount;
            ViewBag.CurrentPageIndex = currentPageIndex;

            return View(model);
        }

        public async Task<IActionResult> Add()
        {


            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Add(Employee model)
        {
            var resp = await employeeService.GetByPhoneNum(model.PhoneNum);
            if (resp != null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "已经存在使用该号码已经添加过的用户！", isSuccess = "error" });
            }
            model.UserPwd =Common.Common.CalcMD5(model.UserPwd); //将密码进行MD5加密
            var result = await employeeService.AddNewAsync(model);
            if (result <= 0)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "添加失败！", isSuccess = "error" });

            }

            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "添加成功！", isSuccess = "ok" });

        }

        public async Task<IActionResult> GetAll()
        {
            //var employess = await employeeService.GetAll();
            //if(employess.Count>0)
            //{
            //    return Content("获取成功");
            //}

            var data = await employeeService.GetAll();

            return Content("获取失败");
        }

        public async Task<IActionResult> GetById()
        {
            //var r = await employeeService.GetById(1);
            //return Content("");
            //EmployeeService es = new EmployeeService();
            //var r = await es.GetByIdAsync(1);

            //commonService.

            var r = await employeeService.GetByIdAsync(1);

            return Content("");
        }


        public async Task<IActionResult> Edit(int id)
        {
            var model = await employeeService.GetByIdAsync(id);
            if (model == null)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "不存在该用户！", isSuccess = "error" });

            }
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(EmployeeDTO model)
        {
            model.UserPwd=Common.Common.CalcMD5(model.UserPwd);
            var resp = await employeeService.Update(model);
            if (resp <= 0)
            {
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "修改失败！", isSuccess = "error" });
            }
            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "修改成功！", isSuccess = "OK" });

        }

        public async Task<IActionResult> BatchDeleted(string ids)
        {
            string[] empIds =  ids.Split(',');
            List<int> deletedResult = new List<int>();
            List<int> deletedResultIds = new List<int>();
            foreach (var id in empIds)
            {
               var data =await employeeService.GetByIdAsync(Convert.ToInt32(id));
                if(data!=null)
                {
                   var result = await employeeService.MarkDeleted(data.Id);
                    deletedResult.Add(result);
                    deletedResultIds.Add(data.Id);
                }
            }

            if(deletedResult.Contains(-1))
            {
                string deletedFaillids = "";
                foreach (var id in deletedResultIds)
                {
                    deletedFaillids += id + "-";
                }
                return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "删除失败！删除失败的编号为：" + deletedFaillids, isSuccess = "OK" });
            }

            return RedirectToAction("Tip", "Home", new { redirectUrl = "/Employee/List", msg = "删除成功！", isSuccess = "OK" });

        }
    }
}